package com.baosight.sczx.zm.cl.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.zm.cl.domain.ZMCLReport_RiskRecord;
import com.baosight.sczx.zm.cl.domain.ZMCLReport_SafetyReport;

/**
 * 安全月报模块(数据维护)
 * 主表
 * <p>Title: ServiceZMCLR1.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 严洪浩
 * @version 1.0
 * <pre>Histroy:
 *          2011-11-18 上午08:56:16  严洪浩  Create
 *</pre>
 */
public class ServiceZMCLR1 extends ServiceEPBase {
	
	/**
	 * 安全信息月报表 进入初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCL57</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-30              高强       
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo)
	{
		EiInfo outInfo;
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 安全信息月报记录 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2011-5-30               高强       
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo)
	{
		StaticTools.setQueryParameters(inInfo, "fdType", "1", true);//添加查询条件
		EiInfo queryInfo = super.query(inInfo, "ZMCLReport_SafetyReport.query", new ZMCLReport_SafetyReport());
		EiInfo outInfo = new EiInfo();
		if(queryInfo.getBlock(EiConstant.resultBlock).getRowCount() == 0)
		{
			outInfo.addBlock(EiConstant.resultBlock).addBlockMeta(new ZMCLReport_SafetyReport().eiMetadata);
		} else {
			outInfo = queryInfo;
		}
		return outInfo;
	}
	
	/** 新增隐患月报
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-5-30  高强
	 * </p>
	 *
	*/
	@Override
	public EiInfo insert(EiInfo inInfo) {
		Map<String, String> params = new HashMap<String, String>();
		String reportDate = inInfo.getBlock("result").getCellStr(0, "fdYear")+inInfo.getBlock("result").getCellStr(0, "fdMoth").toString();
		params.put("fdReportDate",reportDate );
		boolean validateValue = validateReport(inInfo,params);//有效性确认
		if(validateValue) {
			return inInfo;
		}
		inInfo.getBlock("result").setCell(0, "fdKeyId", TablePk.getInstance().getNextValue("T_CL_REPORTASSOCIATION"));//主键
		inInfo.getBlock("result").setCell(0, "fdReportDate", reportDate);//报表日期
		EiInfo outInfo = super.insert(inInfo, "ZMCLReport_SafetyReport.insert", "result");
		return outInfo;
	}

	/**
	 * 更新发布状态为"生效"的方法
	 * <p>Function: updateStatus</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *     2011-6-2  高强
	 * </p>
	 *
	 */
	
	public EiInfo updateStatus(EiInfo inInfo) 
	{
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdReportId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		boolean validateValue = canPublish(inInfo,params);//有效性验证
		if(validateValue) {
			return inInfo;
		}
		EiInfo _temp = super.update(inInfo, "ZMCLReport_SafetyReport.updateStatus");
        EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 安全信息月报 删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-2  高强
	 * </p>
	 *
	*/
	@Override
	public EiInfo delete(EiInfo inInfo)
	{
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdReportId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		boolean validateValue = canDelete(inInfo,params);//有效性验证
		if(validateValue) {
			return inInfo;
		}
		EiInfo _temp = super.delete(inInfo, "ZMCLReport_SafetyReport.delete");
		return query(_temp);
	}

	
	/**
	 * 检查是否有重复的月报存在
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-28下午         高强
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validateReport(EiInfo inInfo, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		List result = dao.query("ZMCLReport_SafetyReport.query", params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCLReport_SafetyReport data = (ZMCLReport_SafetyReport)result.get(0);

			inInfo.setMsg("日期为" +data.getFdReportDate()+ "的月报已经存在！");
			return true;
		}
		return false;
	}
	
	/**
	 * 检查是否可以删除月报
	 * <p>Function: canDelete</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-7下午01:32:59 高强
	 * </p>
	 *
	 */
	@SuppressWarnings("unchecked")
	private boolean canDelete(EiInfo inInfo, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		List<ZMCLReport_RiskRecord> result = dao.query("ZMCLReportRiskRecord.query", params);
		if(result!=null && result.size() > 0) {
			inInfo.setStatus(-1);

			inInfo.setMsg("月报下存在隐患项目不能删除！");
		   return true;
		}
		return false;
	}
	
	/**
	 * 检查是否可以发布月报
	 * <p>Function: canPublish</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-3-7下午01:32:59 高强
	 * </p>
	 *
	 */
	@SuppressWarnings("unchecked")
	private boolean canPublish(EiInfo inInfo, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		List<ZMCLReport_RiskRecord> result = dao.query("ZMCLReportRiskRecord.query", params);
		if(result!=null && result.size() == 0) {
			inInfo.setStatus(-1);

			inInfo.setMsg("不存在隐患项目，不能发布！");
		   return true;
		}
		return false;
	}
	
}
